package xyz.yuzegod.megawalls.classes.novice.enderman;

import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.block.BlockBreakEvent;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.CollectSkill;
import xyz.yuzegod.megawalls.game.Game;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.ItemUtils;
import xyz.yuzegod.megawalls.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class FourthSkill extends CollectSkill
{
    public FourthSkill(final Classes classes) {
        super("末影方块", classes);
    }
    
    @Override
    public int maxedLevel() {
        return 3;
    }
    
    @Override
    public double getAttribute(final int level) {
        switch (level) {
            case 1: {
                return 0.12;
            }
            case 2: {
                return 0.24;
            }
            case 3: {
                return 0.36;
            }
            default: {
                return 0.12;
            }
        }
    }
    
    @Override
    public List<String> getInfo(final int level) {
        final List<String> lore = new ArrayList<String>();
        if (level == 1) {
            lore.add(" §8▪ §7当你挖掘矿石和木头时,");
            lore.add("    §7你有§a" + StringUtils.percent(this.getAttribute(level)) + "§7的几率立即挖掘");
            lore.add("    §7周围的所有方块。");
            return lore;
        }
        lore.add(" §8▪ §7当你挖掘矿石和木头时,");
        lore.add("    §7你有§8" + StringUtils.percent(this.getAttribute(level - 1)) + " ➜");
        lore.add("    §a" + StringUtils.percent(this.getAttribute(level)) + "§7的几率立即挖掘");
        lore.add("    §7周围的所有方块。");
        return lore;
    }
    
    @Override
    public void upgrade(final GamePlayer gamePlayer) {
        final KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(this.getClasses());
        kitStats.addSkill4Level();
    }
    
    @Override
    public int getPlayerLevel(final GamePlayer gamePlayer) {
        return gamePlayer.getPlayerStats().getKitStats(this.getClasses()).getSkill4Level();
    }
    
    @Override
    public void onBlockBreak(final KitStatsContainer kitStats, final BlockBreakEvent e) {
        if ((ItemUtils.exceptDirt(e.getBlock())) && MegaWalls.getRandom().nextInt(100) <= this.getAttribute(kitStats.getSkill4Level()) * 100.0) {
            final Game game = MegaWalls.getInstance().getGame();
            final Block block = e.getBlock();
            final BlockFace[] blockFaces = { BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.WEST, BlockFace.EAST };
            boolean breaked = false;
            for (final BlockFace blockFace : blockFaces) {
                final Block relative = block.getRelative(blockFace);
                if (!game.isUnbreakable(relative.getLocation()) && relative.getType() != Material.FURNACE && relative.getType() != Material.TRAPPED_CHEST) {
                    if (relative.getType() != Material.BEDROCK) {
                        relative.breakNaturally();
                        breaked = true;
                    }
                }
            }
            if (breaked) {
                GamePlayer.get(e.getPlayer().getUniqueId()).playSound(Sound.ENDERMAN_HIT, 1.0f, 1.0f);
            }
        }
    }
}
